clear all
*cap log close
set more off
set seed 603


// files to be read in --------
local NAMEIN1 = "recid_poly.csv"
local NAMEIN2 = "recid_bins.csv"
local NAMEIN3 = "optimal_bins.dta"
local NAMEIN4 = "optimal_fixed.dta"
local NAMEOUT = "table_bootstrap"
// -----------------------------------------------------




//// Initialize LaTeX Table ---------------------
capture erase "${out}/apx_extrap/`NAMEOUT'.tex"
file open fh using "${out}/apx_extrap/`NAMEOUT'.tex", write replace

file write fh ///	
	"&\multicolumn{4}{c}{\$Y_0\$} && \multicolumn{4}{c}{\$Y_1 - Y_0\$}\\" _n /// 
	"\cmidrule(l){2-5} \cmidrule(l){7-10}" _n ///
	"& (1) & (2) & (3) & (4) && (5) & (6) & (7) & (8)\\" _n ///
	"& All & \$D=1\$ & \$D=0\$ & Diff && All & \$D=1\$ & \$D=0\$ & Diff \\" _n ///
	"&     &         &         &      && (ATE) & (ATT) & (ATU) &  \\" _n ///
	"\hline" _n
	

// first panel: quadratic polynomial -------
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel A: Quadratic polynomial}}}  \\"  _n ///
	
import delimited using "${est}/`NAMEIN1'", clear
keep if poly==2

replace par = trim(upper(par))
gen g = 1
local glab1 = "    "

foreach q in 1 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

}	


// second panel: fixed 5% bins -------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel B: Fixed \$bw = 0.05\$}}}  \\"  _n ///
	
import delimited using "${est}/`NAMEIN2'", clear
keep if int(bw*100)==5 & type=="mean"

replace par = trim(upper(par))
gen g = 1
local glab1 = "    "

foreach q in 1 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

}	


// third panel: fixed optimal bins -------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel C: Fixed optimal bins}}}  \\"  _n ///
	

use "${est}/`NAMEIN3'", clear
*keep if yvar == "cite_ny1"
replace par = trim(upper(par))
gen g = 1
local glab1 = "    "

foreach q in 1 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

}	


// fourth panel: variable optimal bins bins -------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel D: Variable optimal bins}}}  \\"  _n ///
	

use "${est}/`NAMEIN4'", clear
*keep if yvar == "cite_ny1"
replace par = trim(upper(par))
gen g = 1
local glab1 = "    "

foreach q in 1 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

}	


file write fh "\bottomrule" _n  
file close fh
macro drop fh




